<template>
  <div class="statistics-panel">
    <!-- 本周概览标题 -->
    <div class="panel-header">
      <div class="header-left">
        <h2 class="panel-title">{{ currentListName }}</h2>
        <div class="task-stats">
          <span class="stat-item">{{ currentTasks.length }} 个任务</span>
          <span class="stat-item">{{ currentTasks.filter(t => !t.completed).length }} 个待完成</span>
          <span class="stat-item">{{ currentTasks.filter(t => t.completed).length }} 个已完成</span>
        </div>
      </div>
    </div>
    
    <!-- 统计数据 -->
    <div class="statistics-grid">
      <div class="stat-item">
        <div class="stat-number estimated-time">{{ statistics.estimatedTime }}</div>
        <div class="stat-label">预计时间</div>
        <div class="stat-unit">分钟</div>
      </div>
      
      <div class="stat-item">
        <div class="stat-number pending-tasks">{{ statistics.pendingTasks }}</div>
        <div class="stat-label">待完成任务</div>
      </div>
      
      <div class="stat-item">
        <div class="stat-number completed-time">{{ statistics.completedTime }}</div>
        <div class="stat-label">已完成时间</div>
        <div class="stat-unit">分钟</div>
      </div>
      
      <div class="stat-item">
        <div class="stat-number completed-tasks">{{ statistics.completedTasks }}</div>
        <div class="stat-label">已完成任务</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useTodoStore } from '../../stores/todoStore'

// 使用todoStore
const { state, statistics: stats, currentTasks: storeCurrentTasks } = useTodoStore()

// 计算统计数据
const statistics = computed(() => {
  const tasks = currentTasks.value
  const pendingTasks = tasks.filter(task => !task.completed)
  const completedTasks = tasks.filter(task => task.completed)
  
  // 计算已完成时间：优先使用pomodoroCompleted，如果不存在则使用pomodoroCount
  const completedTime = completedTasks.reduce((total, task) => {
    const completedPomodoros = task.pomodoroCompleted || task.pomodoroCount || 0
    const timeInMinutes = completedPomodoros * 25
    
    // 添加调试信息
    // if (task.completed) {
    //   console.log('📊 统计已完成任务:', {
    //     taskId: task.id,
    //     title: task.title || task.text,
    //     pomodoroCount: task.pomodoroCount,
    //     pomodoroCompleted: task.pomodoroCompleted,
    //     usedPomodoros: completedPomodoros,
    //     timeInMinutes: timeInMinutes
    //   })
    // }
    
    return total + timeInMinutes
  }, 0)
  
  const result = {
    estimatedTime: pendingTasks.reduce((total, task) => total + ((task.pomodoroRequired || 1) * 25), 0),
    pendingTasks: pendingTasks.length,
    completedTime: completedTime,
    completedTasks: completedTasks.length
  }
  
  // 添加总体统计调试信息
  console.log('📈 统计数据更新:', {
    totalTasks: tasks.length,
    pendingTasks: result.pendingTasks,
    completedTasks: result.completedTasks,
    estimatedTime: result.estimatedTime,
    completedTime: result.completedTime,
    completedTasksDetails: completedTasks.map(task => ({
      id: task.id,
      title: task.title || task.text,
      pomodoroCount: task.pomodoroCount,
      pomodoroCompleted: task.pomodoroCompleted
    }))
  })
  
  return result
})

const currentTasks = computed(() => {
  // 使用todoStore的currentTasks，它已经过滤了已删除的任务
  return storeCurrentTasks.value
})

const currentListName = computed(() => {
  if (state.currentView === 'today') return '今天'
  if (state.currentView === 'tomorrow') return '明天'
  if (state.currentView === 'thisweek') return '本周'
  if (state.currentView === 'planned') return '所有任务'
  if (state.currentView === 'high') return '高优先级'
  if (state.currentView === 'medium') return '中优先级'
  if (state.currentView === 'low') return '低优先级'
  if (state.currentView === 'completed') return '已完成'

  
  const currentList = state.lists.find(list => list.id === state.currentListId)
  return currentList ? currentList.name : '任务'
})
</script>

<style lang="less" scoped>
.statistics-panel {
  background-color: var(--background-color);
  border-bottom: 1px solid var(--border-color);
  padding: 0 0 24px 0;
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 24px 16px;
    border-bottom: 1px solid var(--border-color);
    
    .header-left {
      .panel-title {
        margin: 0 0 8px 0;
        font-size: 24px;
        font-weight: 600;
        color: var(--text-primary);
      }
      
      .task-stats {
        display: flex;
        gap: 16px;
        
        .stat-item {
          font-size: 12px;
          color: var(--text-secondary);
        }
      }
    }
    

  }

.statistics-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 32px;
  margin-top: 25px;
}

.stat-item {
  text-align: center;
  
  .stat-number {
    font-size: 48px;
    font-weight: 700;
    line-height: 1;
    margin-bottom: 8px;
    
    &.estimated-time {
      color: var(--primary-color);
    }
    
    &.pending-tasks {
      color: var(--primary-color);
    }
    
    &.total-time {
      color: var(--primary-color);
    }
    
    &.completed-time {
      color: var(--primary-color);
    }
    
    &.completed-tasks {
      color: var(--primary-color);
    }
  }
  
  .stat-label {
    font-size: 14px;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 4px;
  }
  
  .stat-unit {
    font-size: 12px;
    color: var(--text-secondary);
  }
}

@media (max-width: 1024px) {
  .statistics-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 24px;
  }
  
  .stat-item .stat-number {
    font-size: 36px;
  }
}

@media (max-width: 640px) {
  .statistics-panel {
    padding: 16px 20px;
  }
  
  .panel-title {
    font-size: 24px;
  }
  
  .statistics-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }
  
  .stat-item .stat-number {
    font-size: 32px;
  }
}
</style>